package org.or5e.oss.core;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.or5e.oss.core.api.bm.BulkMailConfig;

public final class BaseUtility extends BaseObject {

	private static BaseUtility _utility = new BaseUtility();
	private BaseUtility() {
		
	}
	/**
	 * Factory method, using which any Object of API gain access to this Utility methods.
	 * @return
	 */
	public static BaseUtility getUtility() {
		return _utility;
	}
	
	/**
	 * Utility Method used to verify the string is null and empty
	 * @param _isEmpty
	 * @return
	 */
	public final Boolean isEmptyString(String _isEmpty) {
		if(_isEmpty != null && _isEmpty.trim().equals("")) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	/**
	 * Utility Method used to verify is the String is not null nor empty 
	 * @param _isNotEmpty
	 * @return
	 */
	public final Boolean isNotEmptyString(String _isNotEmpty) {
		return ! isEmptyString(_isNotEmpty);
	}

	/**
	 * Utility Method use to verify is the array is null and empty
	 * @param <T>
	 * @param _array
	 * @return
	 */
	public final <T> Boolean isEmptyArray(T[] _array) {
		if(_array != null && _array.length > 0) {
			return Boolean.FALSE;
		}
		return Boolean.TRUE;
	}

	/**
	 * Utility Method used to verify is the array is not null nor empty
	 * @param <T>
	 * @param _array
	 * @return
	 */
	public final <T> Boolean isNotEmptyArray(T[] _array) {
		return ! isEmptyArray(_array);
	}

	/**
	 * Utility Method used to convert the Date object into String with the specified format (MMDDYY) or anything what the API likes
	 * @param _dt
	 * @param _format
	 * @return
	 */
	public final String getDateAsString(Date _dt, String _format) {
		SimpleDateFormat _dtFormat = new SimpleDateFormat(_format);
		return _dtFormat.format(_dt);
	}

	/**
	 * Utility Method used to convert the Calendar object into String with the specified format (MMDDYY) or anything what the API likes
	 * @param _cal
	 * @param _format
	 * @return
	 */
	public final String getCalAsString(Calendar _cal, String _format) {
		SimpleDateFormat _dtFormat = new SimpleDateFormat(_format);
		return _dtFormat.format(_cal.getTime());
	}

	/**
	 * Utility Method used to Create a Calendar object providing a string date and the format you specify the date format
	 * @param _date
	 * @param _format
	 * @return
	 */
	public final Calendar getStringAsCal(String _date, String _format) throws ParseException {
		Calendar _cal = Calendar.getInstance();
	      if (_date != null && _date.length() > 0)
	      {
	    	  _cal = new GregorianCalendar();
	    	  SimpleDateFormat dateFormat = new SimpleDateFormat(_format);
	         _cal.setTime(dateFormat.parse(_format));
	      }
		return _cal;
	}


	/**
	 * Utility Method used to verify the email address, we use the regular expression to find this ".+@.+\\.[a-z]+"
	 * @param email
	 * @return
	 */
	public boolean isValidEmail(String email) {
		Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
		Matcher m = p.matcher(email);
		return m.matches();
	}

	/**
	 * Utility Method, which is specifically designed for the bulk mail module, which needs to validate the invalid email and and put it into the good and bad file and assign the file names in the Bulk mail configuration object
	 * @param config
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public String[] validateEmail(BulkMailConfig config) throws FileNotFoundException, IOException{
		File mailFile = config.getDataFile();
		String absPath = mailFile.getAbsolutePath();
		BufferedReader mailReader = new BufferedReader(new FileReader(mailFile));
		String email = mailReader.readLine();
		String[] fileList = new String[]{absPath+".Good",absPath+".Bad"};
		BufferedWriter goodFileWriter = new BufferedWriter(new FileWriter(fileList[0]));
		BufferedWriter badFileWriter = new BufferedWriter(new FileWriter(fileList[1]));
		while(email != null) {
			if(isValidEmail(email)) {
				goodFileWriter.write(email+"\n");
			}
			else {
				badFileWriter.write(email+"\n");
			}
			email = mailReader.readLine();
		}
		badFileWriter.close();
		goodFileWriter.close();
		mailReader.close();
		return fileList;
		
	}
	@Override
	public String getName() {
		return "BaseUtility";
	}
}